﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DiPlaner
{
    public partial class BotSetupForm : Form
    {
        #region vars
        public Profile.BotProfile CurentBot = new Profile.BotProfile();
        private string db_path = "";
        private string d3_path = "";
        public static Point syssize = new Point();
        #endregion

        #region External
        [StructLayout(LayoutKind.Sequential)]
        struct RECT
        {
            public int left, top, right, bottom;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct WINDOWINFO
        {
            public uint cbSize;
            public RECT rcWindow;
            public RECT rcClient;
            public uint dwStyle;
            public uint dwExStyle;
            public uint dwWindowStatus;
            public uint cxWindowBorders;
            public uint cyWindowBorders;
            public ushort atomWindowType;
            public ushort wCreatorVersion;

            public WINDOWINFO(Boolean? filler)
                : this()   // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)".
            {
                cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO)));
            }

        }

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
        #endregion

        public BotSetupForm()
        {
            InitializeComponent();
        }

        private void bt_wiz_Click(object sender, EventArgs e)
        {
            int count = DiForm.CurentProfile.WorkPlan.Schedule.Count();


            Form[] fantoms = new Form[count];
            for (int i = 0; i < count; i++)
            {
                fantoms[i] = new FantomMarker();
            }

            WINDOWINFO pwi = new WINDOWINFO();
            GetWindowInfo(this.Handle, ref pwi);
            syssize = new Point()
            {
                X = (pwi.rcClient.left - pwi.rcWindow.left) + (pwi.rcWindow.right - pwi.rcClient.right),
                Y = (pwi.rcWindow.bottom - pwi.rcClient.bottom) + (pwi.rcClient.top - pwi.rcWindow.top)
            };

            using (WinMarker wm = new WinMarker())
            {
                for (int i = 0; i < fantoms.Length; i++)
                {
                    if (i != CurentBot.slot)
                    {
                        fantoms[i].Show();
                        fantoms[i].Text = "Bot #" + (i + 1);
                        fantoms[i].Size = DiForm.CurentProfile.Bots[i].size;
                        fantoms[i].Location = DiForm.CurentProfile.Bots[i].pos;
                    }
                }
                wm.Size = new Size((int)ns_x.Value, (int)ns_y.Value);
                wm.MinimumSize = new Size(200, 150);
                wm.Location = new Point((int)np_x.Value, (int)np_y.Value);
                wm.sys = syssize;
                wm.ShowDialog();
                try
                {
                    np_x.Value = wm.pos.X; np_y.Value = wm.pos.Y;
                    ns_x.Value = wm.size.Width; ns_y.Value = wm.size.Height;
                }
                catch { }

                for (int i = 0; i < fantoms.Length; i++) fantoms[i].Hide();
            }
        }
        private void BotSetupForm_Load(object sender, EventArgs e)
        {
            tx_key.Text = CurentBot.db_key;
            tx_login.Text = CurentBot.bnet;
            tx_pass.Text = CurentBot.pass;
            tx_routine.Text = CurentBot.db_routine;
            d3_path = CurentBot.d3_path;
            db_path = CurentBot.db_path;

            np_x.Value = (CurentBot.pos.X < (int)np_x.Minimum || CurentBot.pos.X > (int)np_x.Maximum)? (int)np_x.Minimum : CurentBot.pos.X;
            np_y.Value = (CurentBot.pos.Y < (int)np_y.Minimum || CurentBot.pos.Y > (int)np_y.Maximum) ? (int)np_y.Minimum : CurentBot.pos.Y;
            ns_x.Value = (CurentBot.size.Width < (int)ns_x.Minimum || CurentBot.size.Width > (int)ns_x.Maximum) ? (int)ns_x.Minimum : CurentBot.size.Width;
            ns_y.Value = (CurentBot.size.Height < (int)ns_y.Minimum || CurentBot.size.Height > (int)ns_y.Maximum) ? (int)ns_y.Minimum : CurentBot.size.Height;
        }

        private void BotSetupForm_Shown(object sender, EventArgs e)
        {
            Checker();
        }

        private void _TextChanged(object sender, EventArgs e)
        {
            Checker();
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            CurentBot.db_key = tx_key.Text;
            CurentBot.bnet = tx_login.Text;
            CurentBot.pass = tx_pass.Text;
            CurentBot.db_routine = tx_routine.Text;

            CurentBot.d3_path = d3_path;
            CurentBot.db_path = db_path;

            CurentBot.pos = new Point((int)np_x.Value, (int)np_y.Value);
            CurentBot.size = new Size((int)ns_x.Value, (int)ns_y.Value);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void path_Click(object sndr, EventArgs e)
        {
            Button sender = (Button)sndr;
            string type = sender.Name.Substring(3, 2);

            switch (type)
            {
                case "d3":
                    openFile.Filter = "Diablo III.exe|*.exe";
                    openFile.Title = "Select Diablo III.exe";
                    openFile.ShowDialog();
                    if (openFile.FileName.Length > 0)
                    {
                        d3_path = openFile.FileName;
                        Checker();
                    }
                    break;
                case "db":
                    openFile.Filter = "Demonbuddy.exe|*.exe";
                    openFile.Title = "Select Demonbuddy.exe";
                    openFile.ShowDialog();
                    if (openFile.FileName.Length > 0)
                    {
                        db_path = openFile.FileName;
                        Checker();
                    }
                    break;
            }
        }

        private void Checker()
        {
            int count = 0;
            if (d3_path != null && d3_path.Length >= 3)
            {
                count++;
                lb_d3_set.Text = "V";
                lb_d3_set.Font = new System.Drawing.Font("Arial", 10, FontStyle.Italic | FontStyle.Bold);
                lb_d3_set.ForeColor = Color.Green;
            }
            else
            {
                lb_d3_set.Text = "X";
                lb_d3_set.Font = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
                lb_d3_set.ForeColor = Color.Red;
            }
            if (db_path != null && db_path.Length >= 3)
            {
                count++;
                lb_db_set.Text = "V";
                lb_db_set.Font = new System.Drawing.Font("Arial", 10, FontStyle.Italic | FontStyle.Bold);
                lb_db_set.ForeColor = Color.Green;
            }
            else
            {
                lb_db_set.Text = "X";
                lb_db_set.Font = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
                lb_db_set.ForeColor = Color.Red;
            }
            if (tx_login.Text != null && tx_login.Text.Length >= 3) count++;
            if (tx_pass.Text != null && tx_pass.Text.Length >= 3) count++;
            if (tx_key.Text != null && tx_key.Text.Length >= 3) count++;
            if (tx_routine.Text != null && tx_routine.Text.Length >= 3) count++;
            if (count >= 6) bt_save.Enabled = true;
            else bt_save.Enabled = false;
        }

        private void ValueChanged(object sender, EventArgs e)
        {
            if (((NumericUpDown)sender).Value > ((NumericUpDown)sender).Maximum) ((NumericUpDown)sender).Value = ((NumericUpDown)sender).Maximum;
            if (((NumericUpDown)sender).Value < ((NumericUpDown)sender).Minimum) ((NumericUpDown)sender).Value = ((NumericUpDown)sender).Minimum;
        }

        private void lb_d3_set_MouseEnter(object sender, EventArgs e)
        {
            toolTip1.SetToolTip((Label)sender, CurentBot.d3_path);
        }

        private void bt_d3_path_MouseEnter(object sender, EventArgs e)
        {
            toolTip1.SetToolTip((Button)sender, CurentBot.d3_path);
        }

        private void lb_db_set_MouseEnter(object sender, EventArgs e)
        {
            toolTip1.SetToolTip((Label)sender, CurentBot.db_path);
        }

        private void bt_db_path_MouseEnter(object sender, EventArgs e)
        {
            toolTip1.SetToolTip((Button)sender, CurentBot.db_path);
        }

    }
}
